﻿using System.Collections.Generic;
using System.Threading.Tasks;
using GrapeCity.Forguncy.Commands;
using TencentCloud.Common;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloudOcrPlugin.Resources;

namespace TencentCloudOcrPlugin.Ocr
{
    public class OcrBizLicenseAction : OcrActionBase
    {
        private static readonly Dictionary<string, string> ParamsDict = new Dictionary<string, string>();

        public OcrBizLicenseAction()
        {
            ParamsDict["RegNum"] = "统一社会信用代码";
            ParamsDict["Name"] = "公司名称";
            ParamsDict["Capital"] = "注册资本";
            ParamsDict["Person"] = "法定代表人";
            ParamsDict["Address"] = "地址";
            ParamsDict["Business"] = "经营范围";
            ParamsDict["Type"] = "主体类型";
            ParamsDict["Period"] = "营业期限";
            ParamsDict["ComposingForm"] = "组成形式";
            ParamsDict["SetDate"] = "成立日期";
            ParamsDict["IsDuplication"] = "是否为副本";
            ParamsDict["RegistrationDate"] = "登记日期";
            ParamsDict["Angle"] = "图片旋转角度";
            ParamsDict["NationalEmblem"] = "是否有国徽";
            ParamsDict["QRCode"] = "是否有二维码";
            ParamsDict["Seal"] = "是否有印章";
            ParamsDict["Title"] = "标题";
            ParamsDict["SerialNumber"] = "编号";
            ParamsDict["RegistrationAuthority"] = "登记机关";
            ParamsDict["Electronic"] = "是否是电子营业执照";
        }
        
        public override List<string> GetParamList(TencentCloudApiPropertyBase properties)
        {
            var list = base.GetParamList(properties);
            list.AddRange(ParamsDict.Values);
            return list;
        }

        public override async Task<Dictionary<string,object>> RunAsync(TencentCloudApiClientConfig clientConfig,
            IServerCommandExecuteContext dataContext, TencentCloudApiPropertyBase properties)
        {
            var businessCardProperties = properties as OcrBizLicenseProperties;
            var baseRequest = await BuildRequestAsync(businessCardProperties, dataContext);
            var request = new BizLicenseOCRRequest()
            {
                ImageBase64 = baseRequest.ImageBase64,
                ImageUrl = baseRequest.ImageUrl
            };
            var response = await GetClient(clientConfig).BizLicenseOCR(request);
            return BuildResp(response, properties);
        }

        private Dictionary<string, object> BuildResp(AbstractModel tencentResp, TencentCloudApiPropertyBase properties) {
            var dict = new Dictionary<string, object>();
            if (!(tencentResp is BizLicenseOCRResponse response))
            {
                return dict;
            }
            if (properties.advancedSettings.TencentRawResp)
            {
                dict["原始数据"] = tencentResp;
            }
            var type = typeof(BizLicenseOCRResponse);
            foreach (var keyValuePair in ParamsDict)
            {
                dict[ParamsDict[keyValuePair.Key]]  = type.GetProperty(keyValuePair.Key)?.GetValue(response);
            }            
            return dict;
        }
    }
}